<?php


namespace app\admin\model;


use my\Security;
use think\Db;
use think\Model;

class YyDriverOnlineTime extends Model
{
    protected $table="yy_driver_online_time";
    //司机在线记录列表
    public static function onlineList($page, $limit, $data = array())
    {
        if (!empty($data['online_start']) && !empty($data['online_end'])) {
            $where['a.start_time'] = ['between', [strtotime($data['online_start']), strtotime($data['online_end'])]];
        } elseif (!empty($data['online_start']) && empty($data['online_end'])) {
            $where['a.start_time'] = ['>=', strtotime($data['online_start'])];
        } elseif (empty($data['online_start']) && !empty($data['online_end'])) {
            $where['a.start_time'] = ['<=', strtotime($data['online_end'])];
        }
        if (!empty($data['offline_start']) && !empty($data['offline_end'])) {
            $where['a.end_time'] = ['between', [strtotime($data['offline_start']), strtotime($data['offline_start'])]];
        } elseif (!empty($data['offline_start']) && empty($data['offline_end'])) {
            $where['a.end_time'] = ['>=', strtotime($data['offline_start'])];
        } elseif (empty($data['offline_start']) && !empty($data['offline_end'])) {
            $where['a.end_time'] = ['<=', strtotime($data['offline_end'])];
        }
        //手机号码
        if (!empty($data['driver_phone'])) {
            $where['b.driver_phone'] = Security::encrypt($data['driver_phone']);
        }
        //分页
        $field = 'a.*,b.driver_phone,b.driver_name';
        $start = ($page - 1) * $limit;
        $count = Db::table('yy_driver_online_time a')->join(['yy_driver_base'=>'b'], 'a.driver_id = b.driver_id', 'LEFT')->where($where)->count();
        $list = Db::table('yy_driver_online_time a')->join(['yy_driver_base'=>'b'], 'a.driver_id = b.driver_id', 'LEFT')->limit($start, $limit)->where($where)->field($field)->order('a.id desc')->select();
        foreach ($list as $key => $value) {
//            $list[$key]['driver_phone'] = Security::decrypt($value['driver_phone']);
            $list[$key]['start_time'] = !empty($value['start_time']) ? date("Y-m-d H:i:s", $value['start_time']) : '-';
            $list[$key]['end_time'] = !empty($value['end_time']) ? date("Y-m-d H:i:s", $value['end_time']) : '-';
            if (!empty($value['city_id'])) {
                $list[$key]['city'] = Db::table('region')->where(['region_id' => $value['city_id']])->value('region_name');
            } else {
                $list[$key]['city'] = '-';
            }
            if (empty($value['end_time'])) {
                $list[$key]['sum_time'] = "-";
            } else {
                $list[$key]['sum_time'] = get_second_h_m($value['end_time'] - $value['start_time']);

            }
        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }
}